home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_qt.idb / usr / freeware / include / qtable.h.z / qtable.h
C/C++ Source or Header  |  2001-04-12  |  13KB  |  435 lines

  1. /****************************************************************************
  2. **
  3. ** Definition of QTable widget class
  4. **
  5. ** Created : 000607
  6. **
  7. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  8. **
  9. ** This file is part of the table module of the Qt GUI Toolkit.
  10. **
  11. ** This file may be distributed under the terms of the Q Public License
  12. ** as defined by Trolltech AS of Norway and appearing in the file
  13. ** LICENSE.QPL included in the packaging of this file.
  14. **
  15. ** This file may be distributed and/or modified under the terms of the
  16. ** GNU General Public License version 2 as published by the Free Software
  17. ** Foundation and appearing in the file LICENSE.GPL included in the
  18. ** packaging of this file.
  19. **
  20. ** Licensees holding valid Qt Enterprise Edition licenses may use this
  21. ** file in accordance with the Qt Commercial License Agreement provided
  22. ** with the Software.
  23. **
  24. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  25. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26. **
  27. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  28. **   information about Qt Commercial License Agreements.
  29. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  30. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  31. **
  32. ** Contact info@trolltech.com if any conditions of this licensing are
  33. ** not clear to you.
  34. **
  35. **********************************************************************/
  36.  
  37. #ifndef QTABLE_H
  38. #define QTABLE_H
  39.  
  40. #ifndef QT_H
  41. #include <qscrollview.h>
  42. #include <qpixmap.h>
  43. #include <qvector.h>
  44. #include <qheader.h>
  45. #include <qarray.h>
  46. #include <qlist.h>
  47. #include <qguardedptr.h>
  48. #include <qshared.h>
  49. #endif // QT_H
  50.  
  51.  
  52. #ifndef QT_NO_TABLE
  53.  
  54. class QTableHeader;
  55. class QValidator;
  56. class QTable;
  57. class QPaintEvent;
  58. class QTimer;
  59. class QResizeEvent;
  60.  
  61.  
  62. struct QTablePrivate;
  63. struct QTableHeaderPrivate;
  64.  
  65.  
  66. class Q_EXPORT QTableSelection
  67. {
  68. public:
  69.     QTableSelection();
  70.     void init( int row, int col );
  71.     void expandTo( int row, int col );
  72.     bool operator==( const QTableSelection &s ) const;
  73.  
  74.     int topRow() const { return tRow; }
  75.     int bottomRow() const { return bRow; }
  76.     int leftCol() const { return lCol; }
  77.     int rightCol() const { return rCol; }
  78.     int anchorRow() const { return aRow; }
  79.     int anchorCol() const { return aCol; }
  80.  
  81.     bool isActive() const { return active; }
  82.  
  83. private:
  84.     uint active : 1;
  85.     uint inited : 1;
  86.     int tRow, lCol, bRow, rCol;
  87.     int aRow, aCol;
  88. };
  89.  
  90.  
  91. class Q_EXPORT QTableItem : public Qt
  92. {
  93. public:
  94.     enum EditType { Never, OnTyping, WhenCurrent, Always };
  95.  
  96.     QTableItem( QTable *table, EditType et, const QString &text );
  97.     QTableItem( QTable *table, EditType et, const QString &text,
  98.         const QPixmap &p );
  99.     virtual ~QTableItem();
  100.  
  101.     virtual QPixmap pixmap() const;
  102.     virtual QString text() const;
  103.     virtual void setPixmap( const QPixmap &p );
  104.     virtual void setText( const QString &t );
  105.     QTable *table() const { return t; }
  106.  
  107.     virtual int alignment() const;
  108.     virtual void setWordWrap( bool b );
  109.     bool wordWrap() const;
  110.  
  111.     EditType editType() const;
  112.     virtual QWidget *createEditor() const;
  113.     virtual void setContentFromEditor( QWidget *w );
  114.     virtual void setReplaceable( bool );
  115.     bool isReplaceable() const;
  116.  
  117.     virtual QString key() const;
  118.     virtual QSize sizeHint() const;
  119.  
  120.     virtual void setSpan( int rs, int cs );
  121.     int rowSpan() const;
  122.     int colSpan() const;
  123.  
  124.     virtual void setRow( int r );
  125.     virtual void setCol( int c );
  126.     int row() const;
  127.     int col() const;
  128.  
  129.     virtual void paint( QPainter *p, const QColorGroup &cg,
  130.             const QRect &cr, bool selected );
  131.  
  132.     void updateEditor( int oldRow, int oldCol );
  133.  
  134. private:
  135.     QString txt;
  136.     QPixmap pix;
  137.     QTable *t;
  138.     EditType edType;
  139.     uint wordwrap : 1;
  140.     uint tcha : 1;
  141.     int rw, cl;
  142.     int rowspan, colspan;
  143.  
  144. };
  145.  
  146.  
  147. #if defined(Q_TEMPLATEDLL)
  148. // MOC_SKIP_BEGIN
  149. template class Q_EXPORT QVector<QTableItem>;
  150. template class Q_EXPORT QVector<QWidget>;
  151. template class Q_EXPORT QList<QTableSelection>;
  152. // MOC_SKIP_END
  153. #endif
  154.  
  155.  
  156. class Q_EXPORT QTable : public QScrollView
  157. {
  158.     Q_OBJECT
  159.     Q_PROPERTY( int numRows READ numRows WRITE setNumRows )
  160.     Q_PROPERTY( int numCols READ numCols WRITE setNumCols )
  161.     Q_PROPERTY( bool showGrid READ showGrid WRITE setShowGrid )
  162.     Q_PROPERTY( bool rowMovingEnabled READ rowMovingEnabled WRITE setRowMovingEnabled )
  163.     Q_PROPERTY( bool columnMovingEnabled READ columnMovingEnabled WRITE setColumnMovingEnabled )
  164.  
  165.     friend class QTableHeader;
  166.  
  167. public:
  168.     QTable( QWidget *parent = 0, const char *name = 0 );
  169.     QTable( int numRows, int numCols,
  170.         QWidget *parent = 0, const char *name = 0 );
  171.     ~QTable();
  172.  
  173.     QHeader *horizontalHeader() const;
  174.     QHeader *verticalHeader() const;
  175.  
  176.     enum SelectionMode { Single, Multi, NoSelection  };
  177.     virtual void setSelectionMode( SelectionMode mode );
  178.     SelectionMode selectionMode() const;
  179.  
  180.     virtual void setItem( int row, int col, QTableItem *item );
  181.     virtual void setText( int row, int col, const QString &text );
  182.     virtual void setPixmap( int row, int col, const QPixmap &pix );
  183.     virtual QTableItem *item( int row, int col ) const;
  184.     virtual QString text( int row, int col ) const;
  185.     virtual QPixmap pixmap( int row, int col ) const;
  186.     virtual void clearCell( int row, int col );
  187.  
  188.     virtual QRect cellGeometry( int row, int col ) const;
  189.     virtual int columnWidth( int col ) const;
  190.     virtual int rowHeight( int row ) const;
  191.     virtual int columnPos( int col ) const;
  192.     virtual int rowPos( int row ) const;
  193.     virtual int columnAt( int pos ) const;
  194.     virtual int rowAt( int pos ) const;
  195.  
  196.     int numRows() const;
  197.     int numCols() const;
  198.  
  199.     void updateCell( int row, int col );
  200.  
  201.     bool eventFilter( QObject * o, QEvent * );
  202.  
  203.     int currentRow() const { return curRow; }
  204.     int currentColumn() const { return curCol; }
  205.     void ensureCellVisible( int row, int col );
  206.  
  207.     bool isSelected( int row, int col ) const;
  208.     bool isRowSelected( int row, bool full = FALSE ) const;
  209.     bool isColumnSelected( int col, bool full = FALSE ) const;
  210.     int numSelections() const;
  211.     QTableSelection selection( int num ) const;
  212.     virtual int addSelection( const QTableSelection &s );
  213.     virtual void removeSelection( const QTableSelection &s );
  214.     virtual void removeSelection( int num );
  215.     virtual int currentSelection() const;
  216.  
  217.     bool showGrid() const;
  218.  
  219.     bool columnMovingEnabled() const;
  220.     bool rowMovingEnabled() const;
  221.  
  222.     virtual void sortColumn( int col, bool ascending = TRUE,
  223.                  bool wholeRows = FALSE );
  224.     bool sorting() const;
  225.  
  226.     virtual void takeItem( QTableItem *i );
  227.  
  228.     virtual void setCellWidget( int row, int col, QWidget *e );
  229.     virtual QWidget *cellWidget( int row, int col ) const;
  230.     virtual void clearCellWidget( int row, int col );
  231.  
  232.     virtual void paintCell( QPainter *p, int row, int col,
  233.                 const QRect &cr, bool selected );
  234.     virtual void paintFocus( QPainter *p, const QRect &r );
  235.     QSize sizeHint() const;
  236.  
  237. public slots:
  238.     virtual void setNumRows( int r );
  239.     virtual void setNumCols( int r );
  240.     virtual void setShowGrid( bool b );
  241.     virtual void hideRow( int row );
  242.     virtual void hideColumn( int col );
  243.     virtual void showRow( int row );
  244.     virtual void showColumn( int col );
  245.  
  246.     virtual void setColumnWidth( int col, int w );
  247.     virtual void setRowHeight( int row, int h );
  248.  
  249.     virtual void adjustColumn( int col );
  250.     virtual void adjustRow( int row );
  251.  
  252.     virtual void setColumnStretchable( int col, bool stretch );
  253.     virtual void setRowStretchable( int row, bool stretch );
  254.     bool isColumnStretchable( int col ) const;
  255.     bool isRowStretchable( int row ) const;
  256.     virtual void setSorting( bool b );
  257.     virtual void swapRows( int row1, int row2 );
  258.     virtual void swapColumns( int col1, int col2 );
  259.     virtual void swapCells( int row1, int col1, int row2, int col2 );
  260.  
  261.     virtual void setLeftMargin( int m );
  262.     virtual void setTopMargin( int m );
  263.     virtual void setCurrentCell( int row, int col );
  264.     void clearSelection( bool repaint = TRUE );
  265.     virtual void setColumnMovingEnabled( bool b );
  266.     virtual void setRowMovingEnabled( bool b );
  267.  
  268. protected:
  269.     void drawContents( QPainter *p, int cx, int cy, int cw, int ch );
  270.     void contentsMousePressEvent( QMouseEvent* );
  271.     void contentsMouseMoveEvent( QMouseEvent* );
  272.     void contentsMouseDoubleClickEvent( QMouseEvent* );
  273.     void contentsMouseReleaseEvent( QMouseEvent* );
  274.     void keyPressEvent( QKeyEvent* );
  275.     void focusInEvent( QFocusEvent* );
  276.     void focusOutEvent( QFocusEvent* );
  277.     void resizeEvent( QResizeEvent * );
  278.     void showEvent( QShowEvent *e );
  279.  
  280.     virtual void paintEmptyArea( QPainter *p, int cx, int cy, int cw, int ch );
  281.     virtual void activateNextCell();
  282.     virtual QWidget *createEditor( int row, int col, bool initFromCell ) const;
  283.     virtual void setCellContentFromEditor( int row, int col );
  284.     virtual QWidget *beginEdit( int row, int col, bool replace );
  285.     virtual void endEdit( int row, int col, bool accept, bool replace );
  286.  
  287.     virtual void resizeData( int len );
  288.     virtual void insertWidget( int row, int col, QWidget *w );
  289.     int indexOf( int row, int col ) const;
  290.  
  291. protected slots:
  292.     virtual void columnWidthChanged( int col );
  293.     virtual void rowHeightChanged( int row );
  294.     virtual void columnIndexChanged( int s, int oi, int ni );
  295.     virtual void rowIndexChanged( int s, int oi, int ni );
  296.     virtual void columnClicked( int col );
  297.  
  298. signals:
  299.     void currentChanged( int row, int col );
  300.     void clicked( int row, int col, int button, const QPoint &mousePos );
  301.     void doubleClicked( int row, int col, int button, const QPoint &mousePos );
  302.     void pressed( int row, int col, int button, const QPoint &mousePos );
  303.     void selectionChanged();
  304.     void valueChanged( int row, int col );
  305.  
  306. private slots:
  307.     void doAutoScroll();
  308.  
  309. private:
  310.     enum EditMode { NotEditing, Editing, Replacing };
  311.  
  312.     void updateGeometries();
  313.     void repaintSelections( QTableSelection *oldSelection,
  314.                 QTableSelection *newSelection,
  315.                 bool updateVertical = TRUE,
  316.                 bool updateHorizontal = TRUE );
  317.     QRect rangeGeometry( int topRow, int leftCol,
  318.              int bottomRow, int rightCol, bool &optimize );
  319.     void fixRow( int &row, int y );
  320.     void fixCol( int &col, int x );
  321.  
  322.     void init( int numRows, int numCols );
  323.     QSize tableSize() const;
  324.     bool isEditing() const;
  325.     void repaintCell( int row, int col );
  326.     void contentsToViewport2( int x, int y, int& vx, int& vy );
  327.     QPoint contentsToViewport2( const QPoint &p );
  328.     void viewportToContents2( int vx, int vy, int& x, int& y );
  329.     QPoint viewportToContents2( const QPoint &p );
  330.  
  331.     void updateRowWidgets( int row );
  332.     void updateColWidgets( int col );
  333.  
  334. private:
  335.     QVector<QTableItem> contents;
  336.     QVector<QWidget> widgets;
  337.     int curRow;
  338.     int curCol;
  339.     QTableHeader *leftHeader, *topHeader;
  340.     EditMode edMode;
  341.     int editCol, editRow;
  342.     QList<QTableSelection> selections;
  343.     QTableSelection *currentSel;
  344.     QTimer *autoScrollTimer;
  345.     bool sGrid, mRows, mCols;
  346.     int lastSortCol;
  347.     bool asc;
  348.     bool doSort;
  349.     bool mousePressed;
  350.     SelectionMode selMode;
  351.     int pressedRow, pressedCol;
  352.     QTablePrivate *d;
  353.  
  354. };
  355.  
  356.  
  357. #if defined(Q_TEMPLATEDLL)
  358. // MOC_SKIP_BEGIN
  359. template class Q_EXPORT QArray<int>;
  360. template class Q_EXPORT QArray<bool>;
  361. // MOC_SKIP_END
  362. #endif
  363.  
  364.  
  365. class Q_EXPORT QTableHeader : public QHeader
  366. {
  367.     Q_OBJECT
  368. public:
  369.     enum SectionState {
  370.     Normal,
  371.     Bold,
  372.     Selected
  373.     };
  374.  
  375.     QTableHeader( int, QTable *t, QWidget *parent=0, const char *name=0 );
  376.     ~QTableHeader() {};
  377.     void addLabel( const QString &s , int size );
  378.  
  379.     void setSectionState( int s, SectionState state );
  380.     SectionState sectionState( int s ) const;
  381.  
  382.     int sectionSize( int section ) const;
  383.     int sectionPos( int section ) const;
  384.     int sectionAt( int section ) const;
  385.  
  386.     void setSectionStretchable( int s, bool b );
  387.     bool isSectionStretchable( int s ) const;
  388.  
  389. signals:
  390.     void sectionSizeChanged( int s );
  391.  
  392. protected:
  393.     void paintEvent( QPaintEvent *e );
  394.     void paintSection( QPainter *p, int index, QRect fr );
  395.     void mousePressEvent( QMouseEvent *e );
  396.     void mouseMoveEvent( QMouseEvent *e );
  397.     void mouseReleaseEvent( QMouseEvent *e );
  398.     void mouseDoubleClickEvent( QMouseEvent *e );
  399.     void resizeEvent( QResizeEvent *e );
  400.  
  401. private slots:
  402.     void doAutoScroll();
  403.     void sectionWidthChanged( int col, int os, int ns );
  404.     void indexChanged( int sec, int oldIdx, int newIdx );
  405.     void updateStretches();
  406.     void updateWidgetStretches();
  407.  
  408. private:
  409.     void updateSelections();
  410.     void saveStates();
  411.     void setCaching( bool b );
  412.     void swapSections( int oldIdx, int newIdx );
  413.     bool doSelection( QMouseEvent *e );
  414.  
  415. private:
  416.     QArray<int> states, oldStates;
  417.     QArray<bool> stretchable;
  418.     QArray<int> sectionSizes, sectionPoses;
  419.     bool mousePressed;
  420.     int pressPos, startPos, endPos;
  421.     QTable *table;
  422.     QTimer *autoScrollTimer;
  423.     QWidget *line1, *line2;
  424.     bool caching;
  425.     int resizedSection;
  426.     bool isResizing;
  427.     int numStretches;
  428.     QTimer *stretchTimer, *widgetStretchTimer;
  429.     QTableHeaderPrivate *d;
  430.  
  431. };
  432.  
  433. #endif // QT_NO_TABLE
  434. #endif // TABLE_H
  435.